<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Counter</title>
  <style>
    .counter{
      margin: 0px auto;
      width: 300px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      background-color: darkorange;
      color: white;
      font-weight: bold;
      border: 2px solid red;
      font-size: 32px;
    }
    .btn{

      margin: 10px auto;
      width: 300px;
      height: 80px;
    }
    .btn button{
      height: 50px;
      width: 30%;
    }
  </style>
</head>
<body>
<div id="counter" class="counter"></div>
<div class="btn">
  <button onclick="start()">开始</button>
  <button onclick="stop()">停止</button>
  <button onclick="reset()">复位</button>
</div>

<script>
  let count = 0;
  //获得counter对象
  let counterObj = document.getElementById("counter");
  //设置counterObj对象标签体的值
  counterObj.innerHTML = count;
  //定义累加函数
  function add(){
    count++;
    counterObj.innerHTML=count;
  }

  let timer=null;//存储定时器对象的变量
  function start(){
    if(timer==null){
      //使用定时器每隔1毫秒调用一次add()函数
      timer = setInterval(add,100);
    }
  }
  function stop(){
    if(timer!=null){
      //清除定时器
      clearInterval(timer);
      timer = null;
    }
  }

  //复位
  function reset(){
    //停止
    stop();
    count = 0;
    counterObj.innerHTML=count;
  }
</script>
</body>
</html>